﻿@namespace BlazorFluentUI
@inherits FluentUIComponentBase
@typeparam TItem

<div class="ms-DetailsRow-fields"
     data-automationid="DetailsRowFields"
     role="presentation">
    @for(var index=0; index<Columns?.Count(); index++)
    {
        var column = Columns?.ElementAt(index);
        string width = "";

        if (double.IsNaN(column!.CalculatedWidth))
        {
            width = "auto";
        }
        else
        {
            width = (column.CalculatedWidth + CellLeftPadding + CellRightPadding + (column.IsPadded ? CellExtraRightPadding : 0)).ToString() + "px";
        }
        // generate a key that auto-dirties when content changes, to force the container to re-render, to trigger animation
        //string key = $"{column.Key}${(cellValueKey != null ? "-${ cellValueKey}" : "")}";
        <div
             role=@(column.IsRowHeader ? "rowheader" : "gridcell")
             aria-readonly="true"
             aria-colindex=@(index + ColumnStartIndex + 1)
             class=@($"ms-DetailsRow-field{(column.IsMultiline ? " is-multiline":"")}{(column.IsRowHeader ? " is-row-header":"")} ms-DetailsRow-cell{(column.IsPadded ? " ms-DetailsRow-cellPadded":" ms-DetailsRow-cellUnpadded")}")
             style=@($"width:{width};padding-left:{CellLeftPadding}px;")

             >
            @if (column.InternalColumnItemTemplate != null && column.FieldSelectorExpression != null)
            {
                var fragment = (column.InternalColumnItemTemplate as RenderFragment<DynamicAccessor<TItem>>);
                if (fragment != null)
                    @fragment(new DynamicAccessor<TItem>(Item!, column.FieldSelectorExpression!))
            }
            else if (column.InternalColumnItemTemplate != null && column.FieldSelectorExpression == null)
            {
                var fragment = (column.InternalColumnItemTemplate as RenderFragment<TItem>);
                if (fragment != null)
                    @fragment(Item!)
            }
            else
            {
                @: @(column.FieldSelector!(Item!)?.ToString())
            }
        </div>
    }

</div>